<?php

namespace App\Http\Controllers\Admin;
use App\Admin\Arole;
use app\admin\controller\Admin;
use App\Admin\GetCategory;
use App\Admin\Role;
use App\Admin\Rpower;
use App\Admin\Rule;
use Validator;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;

class RoleController extends Controller
{

    //get Admin/role 全部友情列表
    public function index(Request $request)
    {
        $user_id=$request->id;
        $role = Role::select(DB::Raw('role.*,GROUP_CONCAT(power.display_name) as xname'))
            ->leftJoin('rpower', 'rpower.role_id','role.id')
            ->leftJoin('power', 'power.id', 'rpower.rule_id')
            ->groupBy('role.id')->get();
                $role_is = Arole::where('user_id',$user_id)->get()->toArray();
                $role_id=[];
                foreach($role_is as $r){
                    $role_id[] = $r['role_id'];
                }
        return view('admin/role/rolelist',compact('role','user_id','role_id'));
    }
    public function changeOrder(Request $request)
    {
        $input = $request->all();
        $role = Role::find($input['id']);
        $role->order = $input['order'];
        $res = $role->save();
        if($res){
            $data = ['status'=>0,
                'msg'=>'用户排序更新成功',

            ];
        }else{
            $data = [
                'status'=>1,
                'msg'=>'用户排序更新失败，请重试',
            ];
        }
        return $data;
    }
    //get Admin/navs/{navs} 显示单个分类信息
    public function show()
    {

    }
    //get Admin/role/create 添加自定义导航
    public function create()
    {
        return view('admin/role/roleadd');
    }

    //post Admin/role 添加自定义导航 提交
    public function store(Request $request)
    {
        $input = Input::except('_token');
        if($input){
            $rules = [
                'name'=>'required|alpha_dash|between:2,8',
                'order'=>'numeric',
            ];
            $message = [
                'name.required'=>'角色名不能为空!',
                'name.alpha_dash'=>'角色名可以包含字母和数字，以及破折号和下划线。',
                'name.between'=>'角色名必须在5到13位',
                'order.numeric'=>'排序必须是数字',
            ];
            $validator = Validator::make($request->all(),$rules,$message);
            if($validator->passes()){
                $res = Role::create($input);
                if($res){
                    return redirect('admin/role');
                }else{
                    return back()->with('errors','自定义导航添加失败');
                }
            }else{
                return back()->withErrors($validator);
            }

        }
    }
    //get Admin/role/{role}/edit   编辑自定义导航
    public function edit($role_id)
    {
        $role = Role::find($role_id);
        return view('admin/role/roleedit',compact('role'));

    }
    //put Admin/navs/{navs} 更新自定义导航
    public function update($id)
    {
        $input = Input::except('_token','_method');
        $res = Role::where('id',$id)->update($input);
        if($res){
            return redirect('admin/role');
        }else{
            return back()->with('errors','自定义导航更新失败');
        }

    }

    //delete Admin/nav/{nav}  删除自定义导航
    public function destroy($id)
    {
        $res = Role::where('id',$id)->delete();
        if($res){
            Rpower::where('role_id',$id)->delete();
            Arole::where('role_id',$id)->delete();
            $data = [
                'status'=>0,
                'msg'=>'自定义导航删除成功!',
            ];
        }else{
            $data = [
                'status'=>1,
                'msg'=>'自定义导航删除失败!',
            ];
        }
        return $data;
    }

    public function cherg(Request $request)
    {
        if ($request->isMethod('post')) {
            $input = Input::except('_token');
            $rule_id = $input['role_id'];
            $role_id = $input['id'];
            $res = Rpower::where('role_id',$role_id)->delete();
            $role=[];
            foreach($rule_id as $v){
                $rule[] = ['role_id'=>"$role_id",'rule_id'=>"$v"];
            }
            $res = DB::table('rpower')->insert($rule);
            if($res){
                return redirect('admin/role');
            }else{
                return back();
            }
        }
        $role_id = $request->id;
        $role = Role::where('id',$role_id)->select()->get();
        $rule_is = Rpower::where('role_id',$role_id)->get()->toArray();
        $rule_id=[];
        foreach($rule_is as $r){
            $rule_id[] = $r['rule_id'];
        }
        //查询所有的权限
        $power = Rule::all();
        $getCate = new GetCategory();
        $power = (object)$getCate->getTree($power);
//        $power = (object)getTree($power);
        return view('admin.role.middle',compact('power','role','role_id','rule_id'));
    }



}
